<?php
/*

  Copyright (c) 2008   by  maxim.oransky <maxim.oransky@gmail.com>,
  auhtor of  the-wheel-library  team

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

class PageAnalizer {

    private $content;
//    private $hForms;
    private $hInputs;

    public function __construct($content) {
        $this->content = $content;
//        $this->hForms = null;
        $this->hInputs = null;
    }

    public function parseInput($name) {

    	if ($this->hInputs === null) {
    		$this->hInputs = $this->do_parseForm($this->content);
    	}

    	if (!isset($this->hInputs[$name]))
    	   throw new Exception("'$name' not found");

        return $this->hInputs[$name];
    }

    public function parseValue($subRegexp) {

    	if (!preg_match("~$subRegexp~U", $this->content, $m)) {
           echo "<<$this->content>>";
    	   throw new Exception("$subRegexp not found");
    	}

    	return $m[1];
    }

    private function do_parseForm($formContent) {

        //$formContent = $this->getFormContent($formName);
        //echo "=$formContent=";

        $hForm = array();
        $fullRegex = '~<input' .
            self::ANY_TAG_CONTENT_REGEX .
            'name=' . self::QUOTED_VALUE_REGEX.
            self::ANY_TAG_CONTENT_REGEX .
            'value=' . self::QUOTED_VALUE_REGEX.
            self::ANY_TAG_CONTENT_REGEX .
            '>~iu';

        if (preg_match_all($fullRegex, $formContent, $m)) {
            foreach ($m[1] as $i => $name) {
                $name = trim($name, '\'"');
                $hForm[$name] = trim($m[2][$i], '\'"');
            }
        }

        return $hForm;
    }

    const QUOTED_VALUE_REGEX = '("[^"]*"|\'[^\']*\')';
    const ANY_TAG_CONTENT_REGEX = '[^>]*';

//    private function getFormContent($formName) {
//
//        if ($this->hForms === null) {
//
//            $this->hForms = array();
//
//            $fullRegex = '~<form' .
//               self::ANY_TAG_CONTENT_REGEX .
//               'name=' . self::QUOTED_VALUE_REGEX.
//               self::ANY_TAG_CONTENT_REGEX .
//               '>(.*)</form>~isuU';
//
//            //print $fullRegex;
//            if (preg_match_all($fullRegex, $this->content, $m)) {
//                foreach ($m[1] as $i => $name) {
//                    $name = trim($name, '\'"');
//                    $this->hForms[$name] = $m[2][$i];
//                }
//            }
//            //print_r($this->hForms);
//        }
//
//        return isset($this->hForms[$formName]) ?
//            $this->hForms[$formName] :
//            'not found';
//    }
}

?>
